<template>
  <section>

    <!--工具条(高级查询)-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.name" placeholder="数据字典名称"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="loadListData">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>


    <!--0.列表-->
    <el-table :data="dicTypes" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange"
              style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="编号" width="200">
      </el-table-column>
      <el-table-column prop="name" label="数据字典名" width="300">
        <template scope="scope">
          <el-button type="text" size="small" @click="table = true"> {{scope.row.name}} </el-button>
        </template>
      </el-table-column>
      <el-table-column prop="sn" label="编号" min-width="300">
      </el-table-column>
      <el-table-column label="操作" width="150">

        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--嵌套表格-->
    <el-drawer title="数据字典明细" :visible.sync="table" direction="rtl" size="50%">
      <!--工具条(高级查询)-->
     <!-- <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
        <el-form :inline="true" :model="filters">
          <el-form-item>
            <el-input v-model="filters.name" placeholder="字典明细名"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="loadListData">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleDetailAdd">新增</el-button>
          </el-form-item>
        </el-form>
      </el-col>-->

      <el-table :data="gridData">
        <el-table-column property="id" label="编号" width="150"></el-table-column>
        <el-table-column property="name" label="明细名" width="300"></el-table-column>
        <el-table-column property="types_id" label="编号" width="150"></el-table-column>
        <el-table-column width="300">
          <div slot="header">
            <el-form :inline="true" :model="filters">
              <el-form-item>
                <el-button type="danger" size="small" @click="handleDetailAdd">新增</el-button>
              </el-form-item>
            </el-form>
          </div>
          <template scope="scope">
            <el-button size="small" @click="handleDetailEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="handleDetailDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--9.工具条(分页)-->
      <el-col :span="24" class="toolbar">
        <el-pagination layout="total, sizes, prev, pager, next"
                       @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :page-sizes=[1,2,3,4,5]
                       :page-size="pageSize"
                       :total="total"
                       style="float:right;">
        </el-pagination>
      </el-col>
    </el-drawer>

    <!--工具条(分页)-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--10.-->
      <el-pagination layout="total, sizes, prev, pager, next"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :page-sizes=[5,10,20,50,100]
                     :page-size="pageSize"
                     :total="total"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--Type新增或修改界面-->
    <el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
      <el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-form-item label="字典名" prop="name">
          <el-input v-model="addForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="addForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--Detail新增或修改界面-->
    <el-dialog :title="title" :visible.sync="addFormVisibleType" :close-on-click-modal="false">
      <el-form :model="addDetailForm" label-width="80px" :rules="addDetailFormRules" ref="addDetailForm">
        <el-form-item label="明细名" prop="name">
          <el-input v-model="addDetailForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="明细编号" prop="types_id">
          <el-input v-model="addDetailForm.types_id" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="addFormVisibleType = false">取消</el-button>
        <el-button type="primary" @click.native="addDetailSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>


<script>

//  默认导出
export default {
  data() {
    return {
      table: false,
      gridData: [],
      //  1.为列表提供数据
      dicTypes:[],
      //  2.加载效果，默认关闭
      listLoading:false,
      //  4.列表选中列
      sels: [],
      //  页码
      page:1,
      //  11.每页展示条数初始值10
      pageSize:10,
      //  12.数据总条数
      total:0,
      //  高级查询
      filters: {
        name: ''
      },
      //  新增或修改的表单是否可见，默认false,只有当新增或修改被点击时才可见
      addFormVisible:false,
      addFormVisibleType:false,
      //新增或修改界面数据
      addForm: {
        name:'',
        sn:''
      },
      //Detail新增或修改界面数据
      addDetailForm: {
        name:'',
        types_id:null
      },
      //  加载效果
      addLoading: false,
      //  新增或修改的规则
      addFormRules: {
        name: [
          { required: true, message: '请输入字典名称', trigger: 'blur' }
        ],
        sn: [
          { required: true, message: '请输入字典编号', trigger: 'blur' }
        ]
      },
      //  新增或修改的规则
      addDetailFormRules: {
        name: [
          { required: true, message: '请输入字典明细名称', trigger: 'blur' }
        ],
        types_id: [
          { required: true, message: '请输入字典明细编号', trigger: 'blur' }
        ]
      },
      //  新增或修改列表的标题
      title:'',
    }
  },
  //  页面所有的方法
  methods: {
    //  双击行  事件
    /*tableCick(val){

    },*/
    //  3.绑定一个方法，选中数据时触发
    selsChange(sels) {
      this.sels = sels;
    },
    //  5.性别显示转换
    //  6.发送请求获取数据
    loadListData(){
      let param = {
        //  当前页页码
        currentPage:this.page,
        //  当前页展示的条数
        pageSize:this.pageSize,
        //  高级查询的关键字
        keywords:this.filters.name
      }
      //  发送post请求获取后端数据
      this.$http.post("/dicType",param).then(res=>{
        console.debug(res);
        //  将后端的数据赋给emps
        this.dicTypes = res.data.data;
        //  13.获取数据后，将总条数赋值给total
        this.total = res.data.totals;
        //  数据响应之后，取消列表加载效果
        this.listLoading = false;
      });

      //  发送axios请求,请求路径为后端接口
      this.$http.get("/dicDetail").then(res=>{
        console.log("明细",res);
        this.gridData = res.data;
        //  后端数据总条数
        this.total = res.data.totals;
        //  数据响应之后，取消列表加载效果
        this.listLoading = false;
      })
    },
    //  14.增加翻页的方法
    handleCurrentChange(val){
      this.page = val;
      //  刷新列表
      this.loadListData();
    },
    //  15.页码改变事件
    handleSizeChange(val){
      this.pageSize = val;
      //  刷新列表
      this.loadListData();
    },
    //  删除
    handleDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //  开启加载效果
        this.listLoading = true;
        console.debug(row.id);
        //  发送delete请求
        this.$http.delete("/dicType/"+row.id).then(res=>{
          console.debug(res);
          //  解构表达式
          let {success,message} = res.data;
          if (success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            //  后台逻辑判断失败  提示信息
            this.$message.error('网络异常');
          }
          //  刷新列表
          this.loadListData();
        })
      }).catch(() => {
        //  取消删除
        this.$message('取消删除');
      });
    },
    //  删除
    handleDetailDel(index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        //  开启加载效果
        this.listLoading = true;
        console.debug(row.id);
        //  发送delete请求
        this.$http.delete("/dicDetail/"+row.id).then(res=>{
          console.debug(res);
          //  解构表达式
          let {success,message} = res.data;
          if (success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            //  后台逻辑判断失败  提示信息
            this.$message.error('网络异常');
          }
          //  刷新列表
          this.loadListData();
        })
      }).catch(() => {
        //  取消删除
        this.$message('取消删除');
      });
    },
    //  批量删除
    batchRemove() {
      //var ids = this.sels.map(item => item.id).toString();
      var ids = this.sels.map(item => item.id);

      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        //  开启加载效果
        this.listLoading = true;
        //  发送批量删除请求
        this.$http.patch("/dicType",ids).then(res=>{
          //  解构表达式
          let {success,message} = res.data;
          if (success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }else {
            //  后台逻辑判断失败  提示信息
            this.$message.error('网络异常');
          }
          //  刷新列表
          this.loadListData();
        })
      }).catch(() => {
        //  取消删除
        this.$message('取消删除');
      });
    },
    //显示新增界面
    handleAdd() {
      //  弹出模态框(添加表单视图)
      this.addFormVisible = true;
      //  新增界面初始值
      this.addForm = {
            name:'',
            sn:''
      };
      this.title='添加数据字典';
    },
    handleDetailAdd() {
      //  弹出模态框(添加表单视图)
      this.addFormVisibleType = true;
      //  新增界面初始值
      this.addDetailForm = {
        name:'',
        types_id:null
      };
      this.title='添加数据字典明细';
    },
    //新增
    addDetailSubmit() {
      this.$refs.addDetailForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //  开启提交的加载效果
            this.addLoading = true;
            //  发送请求，提交数据
            this.$http.put("/dicDetail",this.addDetailForm).then(res=>{
              //  关闭提交的加载效果
              this.addLoading = false;
              //  解构表达式
              let {success,message} = res.data;
              if (success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                //  后台逻辑判断失败  提示信息
                this.$message.error('网络异常');
              }
              //  关闭模态框
              this.addFormVisibleType = false;
              //  刷新列表
              this.loadListData();
            });
          });
        }
      });
    },
    //新增
    addSubmit() {
      this.$refs.addForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            //  开启提交的加载效果
            this.addLoading = true;
            //  发送请求，提交数据
            this.$http.put("/dicType",this.addForm).then(res=>{
              //  关闭提交的加载效果
              this.addLoading = false;
              //  解构表达式
              let {success,message} = res.data;
              if (success){
                this.$message({
                  message: '操作成功',
                  type: 'success'
                });
              }else {
                //  后台逻辑判断失败  提示信息
                this.$message.error('网络异常');
              }
              //  关闭模态框
              this.addFormVisible = false;
              //  刷新列表
              this.loadListData();
            });
          });
        }
      });
    },
    //显示编辑界面
    handleEdit(index, row) {
      //  回显数据
      this.addForm = Object.assign({}, row);
      //  弹出模态框
      this.addFormVisible = true;
      //  将模态框的标题设置为修改
      this.title = '修改数据字典';
    },
    //显示编辑界面
    handleDetailEdit(index, row) {
      //  回显数据
      this.addDetailForm = Object.assign({}, row);
      //  弹出模态框
      this.addFormVisibleType = true;
      //  将模态框的标题设置为修改
      this.title = '修改字典明细';
    },

  },
  //  钩子函数：页面一加载就会调用/执行此函数
  mounted() {
    //  7.加载数据
    this.loadListData();
    loadListDetailData();

  }

}
</script>

<style scoped>

</style>